package com.accp.dao.impl;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;

import com.accp.dao.VacationDao;
import com.accp.pojo.Vacation;

public class VacationDaoImpl extends BaseDaoImpl implements VacationDao {
	
	/**
	 * 申请休假（增加）
	 */
	@Override
	public boolean add(Vacation vacation) {
		boolean flag = false;
		try{
			getSession().save(vacation);
			flag = true;
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return flag;
	}
	/**
	 * 经理查看休假信息
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Vacation> findByManage(String checker,String name) {
		List<Vacation> list = new ArrayList<Vacation>();
		try{
				String hql = "from Vacation where checker =:checker or name =:name";//查询自己和自己员工的休假信息（不查询其他经理）
				list = getSession().createQuery(hql)
									.setString("checker", checker)
									.setString("name", name).list();
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 员工查看休假信息
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Vacation> findByEmployee(String name) {
		List<Vacation> list = new ArrayList<Vacation>();
		try{
			String hql = "from Vacation where name =:name";//普通员工只查看自己休假信息
			list = getSession().createQuery(hql).setString("name", name).list();
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 审核员工休假信息
	 */
	@Override
	public boolean updateaudit(Vacation vacation) {
		boolean flag = false;
		try{
			getSession().update(vacation);
			flag = true;
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return flag;
	}
	/**
	 * 通过ID查看休假信息
	 */
	@Override
	public Vacation findById(Integer id) {
		Vacation vacation = null;
		try{
			vacation = (Vacation) getSession().get(Vacation.class, new Integer(id));
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return vacation;
	}
	/**
	 * 查找（审核人）经理
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<String> findManage() {
		List<String> list = new ArrayList<String>();
		try{
			String hql = "select userName from Users where role = '经理'";
			list = getSession().createQuery(hql).list();
			return list;
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 删除休假信息
	 */
	@Override
	public boolean delete(Vacation vacation) {
		boolean flag = false;
		try{
			getSession().delete(vacation);
			flag = true;
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return flag;
	}
	/**
	 * 删除某个用户的全部休假信息（当删除用户时）
	 */
	@Override
	public boolean deleteAllByUser(String userName) {
		System.out.println("传来的参数为： "+userName);
		boolean flag = false;
		try{
			String hql = "delete from Vacation where name =:userName";
			getSession().createQuery(hql).setString("userName", userName).executeUpdate();
			flag = true;	
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return flag;
	}
	/**
	 * 经理分页查询，查询总页数
	 */
	@Override
	public int totalPage(String className, String checker, String name) {
		String hql = "select count(*) from "+ className + " where checker =:checker or name =:name";
		Query query = this.getSession().createQuery(hql)
				.setString("checker", checker).setString("name", name);
		int total = Integer.parseInt(query.list().get(0).toString());
		return total;
	}
	/**
	 * 经理分页查询，查询首页
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Vacation> selectByPage(String className, int pageNo, int pageSize,
			String checker, String name) {
		Query query = this.getSession().createQuery("from "+ className + " where checker =:checker or name =:name")
				.setString("checker", checker).setString("name", name);
		query.setFirstResult((pageNo - 1) * pageSize);//每页显示的第一条记录
		query.setMaxResults(pageSize);//每页显示的记录数
		return query.list();
	}
	
		
}
